// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

const db = cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  let queryRequest = event.queryRequest;
  let originCollectionName = queryRequest.originCollection;

  return db.collection(originCollectionName)
         .orderBy(queryRequest.orderBy, 'asc')
         .where({
            [queryRequest.matchCondition.matchFieldName] : _[queryRequest.matchCondition.operator](queryRequest.matchCondition.value)
         })
         .limit(queryRequest.limit)
         .get()
         .then(res => {
          let resultSet = res.data;
          console.log(`[数据库] SingleCollectionWithWhereQuery : ${originCollectionName} Query [查询记录] 成功: `, resultSet);
          return resultSet;
         })
}